import { defineStore } from 'pinia';
import { useProductStore } from './productStore';
export const useOrderStore = defineStore('order', {
  state: () => ({
    orders: [],
  }),
  actions: {
    setOrders(orders) {
      this.orders = orders;
    },
    updateOrder(updatedOrder) {
      const index = this.orders.findIndex((order) => order.id === updatedOrder.id);
      if (index !== -1) {
        this.orders[index] = updatedOrder;
      }
    },
    // 可以添加更多操作订单的方法
    fetchOrders() {
      const productStore = useProductStore();
      const orders = [
        {
          id: 1,
          orderNumber: 'ORD001',
          productId: 1,
          quantity: 2,
          orderDate: '2023-05-01',
          status: 'shipped',
        },
        {
          id: 2,
          orderNumber: 'ORD002',
          productId: 2,
          quantity: 1,
          orderDate: '2023-05-02',
          status: 'notShipped',
        },
        {
          id: 3,
          orderNumber: 'ORD003',
          productId: 3,
          quantity: 3,
          orderDate: '2023-05-03',
          status: 'completed',
        },
      ];
      this.setOrders(
        orders.map((order) => ({
          ...order,
          product: productStore.getProductById(order.productId),
        }))
      );
    },
  },
  getters: {
    getFilteredOrders: (state) => (filter) => {
      if (filter === 'all') return state.orders;
      return state.orders.filter((order) => order.status === filter);
    },
  },
});
